package cn.picclife.mwx.manager.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.picclife.mwx.common.web.base.BaseResult;
import cn.picclife.mwx.manager.sys.controller.TipsMatchController;
import cn.picclife.mwx.manager.sys.mapper.ApprovalMapper;
import cn.picclife.mwx.manager.sys.model.bo.ApprovalInfo;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.service.ApprovalService;

@Service
public class ApprovalServiceImpl implements ApprovalService {
	private Log log = LogFactory.getLog(ApprovalServiceImpl.class);
	@Value("${branch.secret}")
	private String secret;

    @Autowired
    private ApprovalMapper approvalMapper;

    @Override
    public BaseResult applySubmit(ApprovalInfo approvalInfo) {
        BaseResult baseResult = new BaseResult();
        try {
            approvalMapper.insertApprovalInfo(approvalInfo);
        }catch (Exception e){
            log.info(e);
            baseResult.setRet(BaseResult.BIZ_ERROR);
            baseResult.setMsg("保存失败");
            return baseResult;
        }
        baseResult.setRet(BaseResult.SUCCESS);
        baseResult.setMsg("保存成功");
        return baseResult;
    }

    @Override
    public ApprovalInfo queryApprovalInfo(ApprovalInfo approvalInfo) {
        BaseResult baseResult = new BaseResult();
        ApprovalInfo approvalInfo1 = new ApprovalInfo();
        try {
            log.info("222");
            approvalInfo1 = approvalMapper.selectApprovalInfo(approvalInfo);
        } catch (Exception e) {
            log.info(e);
            baseResult.setRet(BaseResult.BIZ_ERROR);
            baseResult.setMsg("查询失败");
        }
        return approvalInfo1;
    }

    @Override
    public PageCount queryByReqDetail(ApprovalInfo approvalInfo,String deptNo) {
    	PageCount pageCount = new PageCount();
		pageCount.setNowPage(approvalInfo.getNowPage());//当前页
		pageCount.setRowNum(approvalInfo.getRowNum());//每页多少条
		//查询总条数
		Map<String,Object> datemap = new HashMap<String,Object>();
		if(secret.equals("true")){
			datemap.put("deptNo", deptNo);//用户机构
			approvalInfo.setDeptNo(deptNo);
		}
		datemap.put("reqDetail", approvalInfo.getReqDetail());
		datemap.put("optName", approvalInfo.getOptName());//投保人姓名
		datemap.put("appType", approvalInfo.getAppType());//申请类型
		datemap.put("oprator", approvalInfo.getOprator());//当前登录人

		//总数
		int allCount = approvalMapper.selectCountByReqDetail(approvalInfo);
		if(0==pageCount.getRowNum()) {
			pageCount.setRowNum(10);
		}
		if(0==pageCount.getNowPage()) {
			pageCount.setNowPage(1);
		}
		int allPages = allCount / pageCount.getRowNum();

		if (allCount % pageCount.getRowNum() > 0) {
			allPages += 1;//总页数
		}
		pageCount.setAllPages(allPages);
		pageCount.setAllCount(allCount);
		int minTotal = (pageCount.getNowPage() - 1) * pageCount.getRowNum();
		int maxTotal = pageCount.getNowPage() * pageCount.getRowNum();
		pageCount.setMinTotal(minTotal);
		pageCount.setMaxTotal(maxTotal);
		pageCount.setMaxminTotal(maxTotal-minTotal);
		pageCount.setObject(datemap);
		List<ApprovalInfo> list = approvalMapper.selectByReqDetail(pageCount);
		pageCount.setDatas(list);
    	return pageCount;
    }

    @Override
    public ApprovalInfo queryByOptName(ApprovalInfo approvalInfo) {
        ApprovalInfo approvalInfo1 = approvalMapper.selectByOptName(approvalInfo);
        return approvalInfo1;
    }

    @Override
    public BaseResult updateApprovalInfo(ApprovalInfo approvalInfo) {
        BaseResult baseResult = new BaseResult();
        try {
            approvalMapper.updateApprovalInfo(approvalInfo);
        } catch (Exception e) {
            log.info(e);
            baseResult.setRet(BaseResult.BIZ_ERROR);
            baseResult.setMsg("插入失败");
            return baseResult;
        }
        baseResult.setRet(BaseResult.SUCCESS);
        baseResult.setMsg("插入成功");
        return baseResult;
    }
	@Override
	public BaseResult updateApplyInfo(ApprovalInfo approvalInfo)
			throws Exception {
		 BaseResult baseResult = new BaseResult();
        try {
        	List<ApprovalInfo> list = approvalMapper.selectBycollectId(approvalInfo);
        	if(list.size() == 0){
        		approvalMapper.insertApprovalInfo(approvalInfo);
        	}else{
        		approvalMapper.updateApplyInfo(approvalInfo);
        	}
        	
        } catch (Exception e) {
            log.info(e);
            baseResult.setRet(BaseResult.BIZ_ERROR);
            baseResult.setMsg("插入失败");
            return baseResult;
        }
        baseResult.setRet(BaseResult.SUCCESS);
        baseResult.setMsg("插入成功");
        return baseResult;
	}
}
